package com.rongc.client.freight.business.supply.view.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.StringUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseFragment;
import com.rongc.client.freight.base.FragmentCallBack;
import com.rongc.client.freight.base.view.adapter.RecyclerBaseAdapter;
import com.rongc.client.freight.base.view.widget.EmptyRecyclerView;
import com.rongc.client.freight.base.view.widget.PullToRefreshEmptyRecyclerView;
import com.rongc.client.freight.business.carrier.model.ConveyanceBean;
import com.rongc.client.freight.business.supply.view.activity.SupplyConveyanceDetailActivity;
import com.rongc.client.freight.business.supply.view.adapter.RecyclerSupplyConveyanceAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by Bruce on 2016/11/24.
 */
public class SupplyConveyanceFragment extends BaseFragment {
    @Bind(R.id.recycler)
    PullToRefreshEmptyRecyclerView mRecyclerView;
    RecyclerSupplyConveyanceAdapter modelAdapter;

    @Bind(R.id.id_empty_view)
    View mEmptyView;
    List<ConveyanceBean> datas;
    String id; // 货源id
    String price;
    int page = 1;
    PullToRefreshBase.Mode mode = PullToRefreshBase.Mode.BOTH;

    public static SupplyConveyanceFragment getInstance(){
        SupplyConveyanceFragment fragment = new SupplyConveyanceFragment();
        Bundle bdl = new Bundle();
//        bdl.putString("orderType", orderType);
        fragment.setArguments(bdl);
        return fragment;
    }

    public static SupplyConveyanceFragment getInstance(String id){
        SupplyConveyanceFragment fragment = new SupplyConveyanceFragment();
        Bundle bdl = new Bundle();
//        bdl.putString("orderType", orderType);
        fragment.setArguments(bdl);
        fragment.id = id;
        return fragment;
    }

    public void setMode(PullToRefreshBase.Mode mode){
        this.mode = mode;
    }

    public void setDatas(List<ConveyanceBean> datas, int page){
        if(page == 1){
            this.datas.clear();
        }
        if(datas!=null){

            this.datas.addAll(datas);
        }
        this.page = page;
        if(this.datas == null || this.datas.size()==0)
            mRecyclerView.getRefreshableView().setEmptyView(mEmptyView);
        mRecyclerView.onRefreshComplete();
        modelAdapter.notifyDataSetChanged();
    }

    public void setPrice(String price){
        this.price = price;
    }


    @Override
    public void initData() {
    }

    RecyclerBaseAdapter.OnItemClickListener mListener;
    public void setListener(RecyclerBaseAdapter.OnItemClickListener mListener){
        this.mListener = mListener;
    }

    @Override
    public void initListener() {
        mRecyclerView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<EmptyRecyclerView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<EmptyRecyclerView> refreshView) {
                Bundle bundle = new Bundle();
                bundle.putInt("page",1);
                ((FragmentCallBack)getActivity()).callbackFun1(bundle);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<EmptyRecyclerView> refreshView) {
                Bundle bundle = new Bundle();
                bundle.putInt("page",++ page);
                ((FragmentCallBack)getActivity()).callbackFun1(bundle);
            }
        });
    }


    @Override
    public void initView() {
        datas = new ArrayList<>();

        modelAdapter = new RecyclerSupplyConveyanceAdapter(getActivity(),datas);
        mRecyclerView.getRefreshableView().setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
//        mRecyclerView.getRefreshableView().addItemDecoration(new SpaceItemDecoration(SystemUtil.dip2px(getContext(), 4f), SystemUtil.dip2px(getContext(), 0f)));
//        mRecyclerView.getRefreshableView().addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.getRefreshableView().setAdapter(modelAdapter);
        mRecyclerView.setMode(mode);

        modelAdapter.setOnItemClickListener(new RecyclerBaseAdapter.OnItemClickListener<ConveyanceBean>() {
            @Override
            public void onItemClick(View view, int position, ConveyanceBean data) {
                if (mListener == null) {
                    Bundle bundle = new Bundle();
                    if (StringUtils.isNotEmpty(id)) {
                        bundle.putString("supplyId", id);
                    }
                    if (StringUtils.isNotEmpty(price)) {
                        bundle.putString("price", price);
                    }
                    bundle.putSerializable("data", data);
                    ActivityUtils.startActivity(getActivity(), SupplyConveyanceDetailActivity.class, bundle);
                }else{
                    mListener.onItemClick(view,position,data);
                }
            }
        });

        if(StringUtils.isNotEmpty(id)){
            ((ImageView)findViewById(R.id.iv_empty)).setImageResource(R.mipmap.icon_none_cheyuan_waybill);
        }else{
            ((ImageView)findViewById(R.id.iv_empty)).setImageResource(R.mipmap.icon_none_cheyuan);
        }

    }


    @Override
    public int getLayoutId() {
        return R.layout.universal_recycler_pull;
    }
}
